package cn.yunhe.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.yunhe.crm.annotation.SystemLog;
import cn.yunhe.crm.entity.Emp;
import cn.yunhe.crm.service.UserinfoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 用户管理
 * @author 尹明泉
 *
 */
@Scope("prototype")
@Controller
public class UserController {
	@Autowired
	@Qualifier("userinfobiz")
	private UserinfoService userbiz;
	public void setUserbiz(UserinfoService userbiz) {
		this.userbiz = userbiz;
	}
	/**
	 * 查询员工集合
	 * @return 员工信息集合
	 */
	@RequestMapping("/userinfo.do")
	public String emplist(Model model) {
		List<Emp> list=userbiz.userinfolist(null);
		model.addAttribute("users", list);
		return "userinfo";
	}
	/**
	 * 	查看员工信息
	 * @param  id 页面的员工ID
	 * @param 把单个员工表(关联着部门和职位和身份)放进model ：emp
	 * @return
	 */
	@RequestMapping("userdetail.do")
	public String detail(Integer id ,Model model) {
		Map<String, Integer> map=new HashMap();
		map.put("id", id);
		List<Emp> list=userbiz.userinfolist(map);
		model.addAttribute("user", list.get(0));
		return "userdetail";
	}
	/**
	 * 修改用户
	 * @return
	 */
	@RequestMapping("/userupdate.do")
	@SystemLog(des="修改用户")
	public String  update(Emp emp) {
		emp.setRoleid(emp.getRole().getId());
		int i=userbiz.useraddorupdate(emp);
		if (i==0) {
			return "error";
		}
		return "forward:/userinfo.do";
	}
	/**
	 * 	跳到修改用户
	 * @param  id 页面的员工ID
	 * @return
	 */
	@RequestMapping("/touserupdate.do")
	public String toupdate(Integer id ,Model model) {
		Map<String, Integer> map=new HashMap();
		map.put("id", id);
		Emp emp=(Emp) userbiz.userinfolist(map).get(0);
		model.addAttribute("user", emp);
		return "userupdate";
	}
	/**
	 * 跳转到添加用户页面
	 * @return
	 */
	@RequestMapping("/usertoadd.do")
	public String toadd() {
		return "useradd";
	}
	/**
	 * 自动补全用户名字和电话
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/checkphone.do")
	public JSONArray checkphone() {
		List<Emp> list=userbiz.unuserlist();
		List list2=new ArrayList();
		if (list.size()>0) {
			for (Emp emp : list) {
				Map map=new HashMap();
				map.put("name", emp.getEmpname());
				map.put("phone", emp.getPhone());
				map.put("id", emp.getId());
				list2.add(map);
			}
		}
		return JSONArray.fromObject(list2);
	}
	/**
	 * 验证码异步刷新
	 * @param session
	 * @param yzm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("checkyzm.do")
	public JSONObject checkyzm(HttpSession session,@RequestParam String yzm){
		Map map=new HashMap();
		String string="";
		String rand=(String) session.getAttribute("rand");
		System.out.println(rand+"-"+yzm);
		if ("".equals(yzm)) {
			string="请输验证码";
		}else if (yzm.equalsIgnoreCase(rand)) {
        	string="输入正确";
		}else{
			string="输入有误";
		}
        map.put("msg", string);
		return JSONObject.fromObject(map);
	}
	
}
